package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundImageView extends ImageView{ 
	public static final int TYPE_RECT = 0;
	public static final int TYPE_ROUND = 1; 
	public static final int TYPE_ROUNDRECT = 2;
	private static final int DEFAULT_CORNER_RADIUS = 6;
	private int mCornerRadius;  //for ROUNDRECT
	private int mRadius; //for ROUND
	private int mType;
	//private int mWidth; 
	private RectF mRoundRect;
	private Matrix mMatrix;  
	private BitmapShader mBitmapShader; 
	private Paint mBitmapPaint; 
	private Context mContext;
	//private Bitmap mBitmap;
	
	public RoundImageView(Context context) {
		this(context, null);
	}
	
	public RoundImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init(attrs);
	}
	
	public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init(attrs);
	}
	
	private void init(AttributeSet attrs){
		mMatrix = new Matrix(); 
		mBitmapPaint = new Paint(); 
		mBitmapPaint.setAntiAlias(true); 
		float density = getResources().getDisplayMetrics().density;
		mType = TYPE_RECT;
		mCornerRadius = (int) (DEFAULT_CORNER_RADIUS * density);
		if(attrs != null){
			TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.RoundImageView); 
			mCornerRadius = a.getDimensionPixelSize(R.styleable.RoundImageView_cornerradius,mCornerRadius); 
			mType = a.getInt(R.styleable.RoundImageView_type, mType);
			a.recycle();  
		}
	}
	
	@Override 
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
		super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
	 
	/**
	* 如果类型是圆形，则强制改变view的宽高一致，以小值为准
	*/ 
		/*int w = getMeasuredWidth();
		int h = getMeasuredHeight();
		if(mType == TYPE_ROUND){ 
			mWidth = Math.min(w, h); 
			mRadius = mWidth / 2; 
			setMeasuredDimension(mWidth, mWidth); 
		} */
	 
	}
	
	public void setType(int type){
		mType = type;
		invalidate();
	}
	
	private Bitmap drawableToBitamp(Drawable drawable){ 
		/*if((mBitmap != null) && (mBitmap.isRecycled() == false)){
			mBitmap.recycle();
			System.gc();
		}*/
		if(drawable instanceof BitmapDrawable) { 
			BitmapDrawable bd = (BitmapDrawable) drawable; 
			return bd.getBitmap(); 
		} 
		/*int w = drawable.getIntrinsicWidth(); 
		int h = drawable.getIntrinsicHeight(); 
		mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
		Canvas canvas = new Canvas(mBitmap); 
		drawable.setBounds(0, 0, w, h); 
		drawable.draw(canvas); 
		return mBitmap; */
		return null;
	}
	
	private void setUpShader(){ 
		Drawable drawable = getDrawable(); 
		if (drawable == null)  
			return; 
	 
		Bitmap bmp = drawableToBitamp(drawable); 
		// 将bmp作为着色器，就是在指定区域内绘制bmp 
		mBitmapShader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP); 
		float scale = 1.0f; 
		if(mType == TYPE_ROUND){ 
			// 拿到bitmap宽或高的小值 
			int w = Math.min(getWidth(), getHeight());
			int bSize = Math.min(bmp.getWidth(), bmp.getHeight()); 
			scale = w * 1.0f / bSize; 
			mRadius = w/2;
		}else if(mType == TYPE_ROUNDRECT){ 
			// 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值； 
			scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight() * 1.0f / bmp.getHeight()); 
		} 
		// shader的变换矩阵，我们这里主要用于放大或者缩小 
		mMatrix.setScale(scale, scale); 
		// 设置变换矩阵 
		mBitmapShader.setLocalMatrix(mMatrix); 
		// 设置shader 
		mBitmapPaint.setShader(mBitmapShader); 
	}
	
	@Override 
	protected void onDraw(Canvas canvas) {
		if(TYPE_RECT == mType){
			super.onDraw(canvas);
			return;
		}
		if(getDrawable() == null)
			return; 
	
		setUpShader(); 
		
		if(mType == TYPE_ROUNDRECT){ 
			mRoundRect = new RectF(0, 0, getWidth(), getHeight());
			canvas.drawRoundRect(mRoundRect, mCornerRadius, mCornerRadius, mBitmapPaint); 
		}else{ 
			canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint); 
		} 
		
	}  

}
